package com.woquyin.model;

import org.apache.commons.beanutils.BeanUtils;

import com.jfinal.plugin.activerecord.Db;
import com.woquyin.common.Constance;
import com.woquyin.common.GrobalRunTimeException;
import com.woquyin.model.baseModel.BaseSecUser;
import com.woquyin.utils.CommonUtil;
import com.woquyin.utils.StringUtils;
import com.woquyin.vo.ReturnData;
import com.woquyin.vo.user.LoginVo;
import com.woquyin.vo.user.SignVo;
import com.woquyin.vo.user.UpdateVo;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class SecUser extends BaseSecUser<SecUser> {
	public static final SecUser dao = new SecUser().dao();

	public ReturnData<String> signService(SignVo signVo) {
		ReturnData<String> returnData = new ReturnData<String>();
		try {
			Constance.UserStateCodeEnum.checkPhone(signVo.getRegPhone());
			Constance.UserStateCodeEnum.checkEmail(signVo.getRegEmail());
			checkVerificationRightOrWrong(signVo.getRegPhone(), signVo.getRegValidateCode());
			checkIsSigned(signVo.getRegPhone());
			saveNewUser(signVo);
			returnData.setCode(Constance.WholeStateCodeEnum.SUCCESS.getCode());
		} catch (GrobalRunTimeException e) {
			returnData.setCode(e.getCode());
			returnData.setMsg(e.getMessage());
		}
		return returnData;
	}
	
	public void checkVerificationRightOrWrong(String phone, String verification){
		if(!CommonUtil.validateCode(phone, verification)){
			throw new GrobalRunTimeException(Constance.UserStateCodeEnum.VERIFICATION_CODE_ERROR.getCode(), Constance.UserStateCodeEnum.VERIFICATION_CODE_ERROR.getValue());
		}
	}
	
	public void checkIsSigned(String phone){
		SecUser user = SecUser.dao.findByPhone(phone);
		if (user != null) {
			throw new GrobalRunTimeException(Constance.UserStateCodeEnum.PHONE_IS_SIGNED.getCode(), Constance.UserStateCodeEnum.PHONE_IS_SIGNED.getValue());
		}
	}
	
	public void saveNewUser(SignVo signVo){
		SecUser user = new SecUser();
		user.setEmail(signVo.getRegEmail());
		user.setJob(signVo.getRegJob());
		user.setPhone(signVo.getRegPhone());
		user.setProfession(signVo.getRegProfession());
		user.setRealName(signVo.getRegRealName());
		user.setId(StringUtils.getUUID());
		user.setWorkUnit(signVo.getRegWorkUnit());
		user.setContactPhone(signVo.getRegContactPhone());
		if(!user.save()){
			throw new GrobalRunTimeException(Constance.UserStateCodeEnum.SIGN_ERROR.getCode(), Constance.UserStateCodeEnum.SIGN_ERROR.getValue());
		}
	}

	public ReturnData<SecUser> loginService(LoginVo vo) {
		ReturnData<SecUser> returnData = new ReturnData<SecUser>();
		//这里应该要验证验证码
		SecUser loginUser = dao.findByPhone(vo.getPhone());
		if (loginUser != null) {
			if (Constance.UserStatus.BE_FROZEN.equals(loginUser.getStatus())) {
				returnData.setCode(Constance.UserStateCodeEnum.BE_FROZEN.getCode());
				returnData.setMsg(Constance.UserStateCodeEnum.BE_FROZEN.getValue());
			} else {
				returnData.setData(loginUser);
				returnData.setCode(Constance.WholeStateCodeEnum.SUCCESS.getCode());
			}
		} else {
			returnData.setCode(Constance.UserStateCodeEnum.VERIFICATION_CODE_ERROR.getCode());
			returnData.setMsg(Constance.UserStateCodeEnum.VERIFICATION_CODE_ERROR.getValue());
		}
		return returnData;
	}

	public ReturnData<SecUser> updateUserInfoService(UpdateVo vo, SecUser userInfo) {
		ReturnData<SecUser> returnData = new ReturnData<SecUser>();
		try {
			Constance.UserStateCodeEnum.checkPhone(vo.getContactPhone());
			Constance.UserStateCodeEnum.checkEmail(vo.getEmail());
			BeanUtils.copyProperties(userInfo, vo);
			updateUserInfoDao(userInfo);
			returnData.setCode(Constance.WholeStateCodeEnum.SUCCESS.getCode());
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof GrobalRunTimeException) {
				GrobalRunTimeException grobalRunTimeException = (GrobalRunTimeException) e;
				returnData.setCode(grobalRunTimeException.getCode());
				returnData.setMsg(grobalRunTimeException.getMessage());
			} else {
				returnData.setCode(Constance.UserStateCodeEnum.UPDATE_USERINFO_ERROR.getCode());
				returnData.setMsg(Constance.UserStateCodeEnum.UPDATE_USERINFO_ERROR.getValue());
			}
		}
		return returnData;
	}

	public void updateUserInfoDao(SecUser userInfo) {
		if (!userInfo.update()) {
			throw new GrobalRunTimeException(Constance.UserStateCodeEnum.UPDATE_USERINFO_ERROR.getCode(),
					Constance.UserStateCodeEnum.UPDATE_USERINFO_ERROR.getValue());
		}
	}

	public SecUser findByPhone(String phone) {
		String sql = "select * from sec_user t where t.phone =?";
		return dao.findFirst(sql, phone);
	}

	public String findStatusById(String id) {
		String sql = "SELECT STATUS FROM sec_user WHERE id =?";
		return Db.queryColumn(sql, id);
	}
	
	public SecUser findUserById(String userId) {
		return dao.findFirst("select * from sec_user t where t.id =?", userId);
	}

	/**
	 * 修改密码
	 */
	// public ApiResult<SecUser> updatePsw(String phone, String password) {
	// ApiResult<SecUser> resp = new ApiResult<SecUser>();
	// if (!StringUtils.isEmpty(phone) && phone.length() != 11) {
	// resp.setCode(ApiResult.APP_ERROR);
	// resp.setMsg("请输入正确的手机号码");
	// } else {
	// SecUser user = SecUser.dao.findByPhone(phone);
	// if (user != null) {
	// user.setPassword(Md5Utils.Md5(password));
	// user.update();
	// resp.setMsg("密码修改成功");
	// } else {
	// resp.setCode(ApiResult.APP_ERROR);
	// resp.setMsg("该手机尚未注册");
	// }
	// }
	// return resp;
	// }

}
